package xxy.com.yangproject.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.DrawableUtils;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.transition.Transition;

import java.util.LinkedHashMap;
import java.util.Random;

import xxy.com.yangproject.R;
import xxy.com.yangproject.global.Constant;

public class AdDialog extends Dialog {
    private LinkedHashMap<Integer, String> linkedHashMap;
    private Context context;
    Random random = new Random();
    private ImageView iv;
    private ImageView iv_close;

    public AdDialog(@NonNull Context context) {
        super(context);
    }

    public AdDialog(@NonNull Context context, int themeResId, LinkedHashMap<Integer, String> linkedHashMap) {
        super(context, themeResId);
        this.linkedHashMap = linkedHashMap;
        this.context = context;
    }

    protected AdDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.eTag(Constant.TAG, "onCreate");
        setContentView(R.layout.ad_dialog);
        iv = findViewById(R.id.iv_ad);
        iv_close = findViewById(R.id.iv_close);
//        iv.setBackgroundResource(src);


        findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        //目的： 根据图片的大小展示弹窗
//        WindowManager.LayoutParams attributes = getWindow().getAttributes();
//        attributes.width = getContext().getResources().getDisplayMetrics().widthPixels / 5;
//        attributes.height = getContext().getResources().getDisplayMetrics().widthPixels / 2;
//        getWindow().setAttributes(attributes);

//        BitmapFactory.Options options;
//        BitmapFactory.decodeResource(context.getResources(), src,  options = new BitmapFactory.Options());
//        int outWidth = options.outWidth;
//        int outHeight = options.outHeight;
//        LogUtils.eTag(Constant.TAG, "outWidth : " + outWidth + "\n" + "outHeight : " + outHeight);
//        WindowManager.LayoutParams attributes = getWindow().getAttributes();
//        attributes.width = outWidth;
//        attributes.height = outHeight;
//        getWindow().setAttributes(attributes);
    }

    @Override
    public void show() {
        super.show();
    }


    /**
     * 后与oncreate() 方法， 先于show（）方法执行
     */
    @Override
    protected void onStart() {
        super.onStart();
        LogUtils.eTag(Constant.TAG, "onStart");
        int i = random.nextInt(3);
        Glide.with(context).asBitmap().autoClone().load(linkedHashMap.get(i + 1))
                .into(new BitmapImageViewTarget(iv) {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        super.onResourceReady(resource, transition);
//                        LogUtils.eTag(Constant.TAG, "resource.getWidth() : " + resource.getWidth() + "\n" + "resource.getHeight() : " + resource.getHeight());
                        int min = Math.min(ScreenUtils.getScreenHeight(), ScreenUtils.getScreenWidth());
                        ViewGroup.LayoutParams layoutParams = iv.getLayoutParams();
                        layoutParams.width = (min * 2 / 3) * layoutParams.width / layoutParams.height ;   //图片高度定位屏幕高度的一半，宽度是屏幕高度的一半乘以图片的宽高比
                        layoutParams.height = min * 2 / 3;
                        iv.setImageBitmap(resource);    //把资源赋值给iv

                        //close imageview
                        //第一次close 不显示
//                        iv_close.setLeft((int) iv.getRight() +10);
//                        iv_close.setBottom((int) iv.getTop() + 10);
                        iv_close.layout(iv.getRight() + 20, iv.getTop() + iv_close.getHeight() + 20, iv.getRight() + 20 + iv_close.getWidth(), iv.getTop() + 20);   //设置view的位置锁定在图片的右上角
                        iv_close.setBackgroundResource(R.drawable.close);
                    }
                });
    }
}
